package im.yangqiang.android.unicorn.http;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import im.yangqiang.android.unicorn.log.ULog;

/**
 * 带日志的StringRequest
 * Created by Carlton on 16/3/12.
 */
public class LogStringRequest extends StringRequest
{
    private static final String TAG = "LogStringRequest";

    public LogStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener)
    {
        super(method, url, listener, errorListener);
    }

    public LogStringRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener)
    {
        super(url, listener, errorListener);
    }

    @Override
    public byte[] getBody() throws AuthFailureError
    {
        byte[] body = super.getBody();
        if(body != null)
        {
            HttpLog.request(getUrl(), new String(body));
        }
        return body;
    }

    @Override
    protected void deliverResponse(String response)
    {
        HttpLog.response(getUrl(), response);
        super.deliverResponse(response);
    }

    @Override
    public void deliverError(VolleyError error)
    {
        HttpLog.error(getUrl(), ULog.getPrintException(error));
        super.deliverError(error);
    }

    @Override
    public void cancel()
    {
        HttpLog.cancel(getUrl());
        super.cancel();
    }
}
